package com.lovo;

import java.util.Scanner;

public class NewATM {

    public static String[][] users = {
            {"aaa","111","500"},
            {"bbb","222","100"},
            {"ccc","333","1000"},
            {"ddd","444","200"}
    };
    public static int index;
    public static Scanner scanner = new Scanner(System.in);
    public static int userID;

    /**
     * 主流程方法
     */
    public static void process(){
        System.out.println("欢迎进入XXX银行");
        while (true){
            userID = -1;
            login();
            menu();
        }

    }

    /**
     * 登录
     */
    public static void login(){

        for (int i = 0; i < 3; i++){
            boolean sign = false;
            System.out.println("请输入账号：");
            String account = scanner.next();
            System.out.println("请输入密码：");
            String pwd = scanner.next();

            for (int j = 0; j < users.length; j++){
                if (account.equals(users[j][0]) && pwd.equals(users[j][1])){
                    sign = true;
                    userID = j;
                    break;
                }
            }
            if (sign){
                System.out.println("登录成功");
                break;
            }else {
                System.out.println("登录失败，你还有" + (2 - i) + "次机会");

            }
            if (i == 2){
                System.out.println("已错误三次，账号冻结");
                System.exit(1);
            }
        }
    }

    /**
     * 菜单
     */
    public static void menu(){
        while(true){
            System.out.println("请选择：1-存款 2-取款 3-转账 4-查询 5-切换账户 6-修改密码 7-退出");
            index = scanner.nextInt();
            if (index == 1){
                save();
            }else if (index == 2){
                get();
            }else if (index == 3){
                transfer();
            }else if (index == 4){
                search();
            }else if (index == 5){
                return;
            }else if (index == 6){
                changePwd();
            }else if (index == 7){
                System.out.println("再见");
                System.exit(0);
            }else {
                System.out.println("输入有误");
            }
        }

    }


    /**
     * 存款
     */
    public static void save(){
        System.out.println("请输入您要存款的金额：");
        double blance = scanner.nextDouble();
        if (blance > 0){
            users[userID][2] = Double.parseDouble(users[userID][2]) + blance + "";
            System.out.println("存款成功！");
        }else {
            System.out.println("金额输入有误");
        }
    }

    /**
     * 取款
     */
    public static void get(){
        System.out.println("请输入您要取款的金额：");
        double blance = scanner.nextDouble();
        if (blance <= Double.parseDouble(users[userID][2]) && blance > 0){
            users[userID][2] = Double.parseDouble(users[userID][2]) - blance + "";
            System.out.println("取款成功！");
        }else {
            System.out.println("取款失败");
        }
    }

    /**
     * 查询
     */
    public static void search(){
        System.out.println("您的存款为" + users[userID][2] + "元");
    }

    /**
     * 转账
     */
    public static void transfer(){
        boolean pd = false;
        int nextUser = -1;
        System.out.println("请输入您要转账的账户：");
        String name = scanner.next();
        if (name.equals(users[userID][0])){
            System.out.println("不能转给自己");
            return;
        }
        for (int j = 0; j < users.length; j++){
            if (name.equals(users[j][0])){
                nextUser = j;
                pd = true;
                break;
            }
        }
        if (pd) {
            System.out.println("请输入转账金额：");
            double blance = scanner.nextDouble();
            if (blance <= Double.parseDouble(users[userID][2]) && blance > 0){
                users[userID][2] = Double.parseDouble(users[userID][2]) - blance + "";
                users[nextUser][2] = Double.parseDouble(users[nextUser][2]) + blance + "";
                System.out.println("转账成功");
            }else {
                System.out.println("转账失败，请检查输入金额或余额");
            }

        }else {
            System.out.println("未查找到相关账户");
        }
    }

    /**
     * 修改密码
     */
    public static void changePwd(){
        boolean bj = false;
        String newPwd1 = "";
        String newPwd2 = "";
        System.out.println("请输入原密码：");
        String pwd = scanner.next();
        if (users[userID][1].equals(pwd)){
            bj = true;
        }else {
            System.out.println("密码错误");
            return;
        }
        if (bj){
            System.out.println("请输入新密码");
            newPwd1 = scanner.next();
            System.out.println("再次输入新密码");
            newPwd2 = scanner.next();
            if (newPwd1.equals(newPwd2)){
                System.out.println("密码修改成功");
                users[userID][1] = newPwd1;
            }else {
                System.out.println("新密码不相同，修改失败");
                return;
            }
        }
    }

    public static void main(String[] args) {
        process();
    }

}
